package com.bwie.book;

import cn.hutool.core.util.RandomUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.apache.commons.collections4.Put;
import org.junit.jupiter.api.Test;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.concurrent.CopyOnWriteArrayList;

/**
 * @Author: badaotianqiong
 * @Date: 2025/6/29 19:55
 * @Description:
 **/
public class BookApp {

    @Test
    public void bookList() {
        ArrayList<Book> books = new ArrayList<>();
        books.add(new Book(1, "阿双丰收", "张三", "阿萨德"));
        books.add(new Book(2, "阿双丰收", "张三", "施工方"));
        books.add(new Book(3, "首都首都", "计划", "速度是"));
        books.add(new Book(4, "刚发的发的", "突然", "胜多负少"));
        books.add(new Book(5, "分光光度法", "实施", "苟富什"));
        System.out.println("图书列表");

        for (Book book : books) {
            System.out.println(book);
        }
    }

    @Test
    public void bookSetList() {
        ArrayList<Book> bookList = new ArrayList<>();
        bookList.add(new Book(1, "阿双丰收", "张三", "阿萨德"));
        bookList.add(new Book(1, "阿双丰收", "张三", "阿萨德"));
        bookList.add(new Book(3, "首都首都", "计划", "速度是"));
        bookList.add(new Book(4, "刚发的发的", "突然", "胜多负少"));
        bookList.add(new Book(5, "分光光度法", "实施", "苟富什"));
        System.out.println("图书列表");
        System.out.println("原始列表:" + bookList.size() + "本");
        HashSet<Book> books = new HashSet<>(bookList);
        books.forEach(System.out::println);
        System.out.println("去重后:" + books.size() + "本");
    }

    @Test
    public void bookMapList() {
        ArrayList<Book> bookList = new ArrayList<>();
        bookList.add(new Book(1, "阿双丰收", "张三", "阿萨德"));
        bookList.add(new Book(2, "的士速递", "首都", "阿萨法"));
        bookList.add(new Book(3, "首都首都", "计划", "速度是"));
        bookList.add(new Book(4, "刚发的发的", "突然", "胜多负少"));
        bookList.add(new Book(5, "分光光度法", "实施", "苟富什"));
        System.out.println("图书列表");

        //bookList.forEach(System.out::println);

        HashMap<String, Book> bookMap = new HashMap<>();
        for (Book book : bookList) {
            bookMap.put(book.getTitle(), book);
        }
        bookMap.forEach((title, book) -> System.out.println(title + "," + book));
    }

    @Test
    public void searchList() {
        ArrayList<Book> bookList = new ArrayList<>();
        bookList.add(new Book(1, "阿双丰收", "张三", "阿萨德"));
        bookList.add(new Book(2, "的士速递", "首都", "阿萨法"));
        bookList.add(new Book(3, "首都首都", "计划", "速度是"));
        bookList.add(new Book(4, "刚发的发的", "突然", "胜多负少"));
        bookList.add(new Book(5, "分光光度法", "实施", "苟富什"));
        System.out.println("图书列表");

        for (Book book : bookList) {
            boolean contains = book.getTitle().contains("发的发");
            if (contains) {
                System.out.println(book);
            }
        }
    }

    @Test
    public void CopyOnWriteArrayList() {
        CopyOnWriteArrayList<Book> bookList = new CopyOnWriteArrayList<>();
        bookList.add(new Book(1, "阿双丰收", "张三", "阿萨德"));
        bookList.add(new Book(2, "的士速递", "首都", "阿萨法"));
        bookList.add(new Book(3, "首都首都", "计划", "速度是"));
        bookList.add(new Book(4, "刚发的发的", "突然", "胜多负少"));
        bookList.add(new Book(5, "分光光度法", "实施", "苟富什"));
        System.out.println("图书列表");

        for (Book book : bookList) {
            System.out.println(book);
        }
    }

    @Test
    public void CopyOnWriteArrayLists() {
        CopyOnWriteArrayList<Book> bookList = new CopyOnWriteArrayList<>();

        new Thread(() -> {
            bookList.add(new Book(1, "阿双丰收", "张三", "阿萨德"));
        });

        new Thread(() -> {
            bookList.add(new Book(2, "的士速递", "首都", "阿萨法"));
        });

        new Thread(() -> {
            bookList.add(new Book(3, "首都首都", "计划", "速度是"));
        });

        try {
            Thread.sleep(1000);
            for (Book book : bookList) {
                System.out.println(book.getTitle());
            }
        } catch (InterruptedException e) {
            e.printStackTrace();
        }

    }

    @Test
    public void ReadArrayList() {
        CopyOnWriteArrayList<Book> bookList = new CopyOnWriteArrayList<>();
        bookList.add(new Book(1, "阿双丰收", "张三", "阿萨德"));
        bookList.add(new Book(2, "的士速递", "首都", "阿萨法"));
        bookList.add(new Book(3, "首都首都", "计划", "速度是"));
        bookList.add(new Book(4, "刚发的发的", "突然", "胜多负少"));
        bookList.add(new Book(5, "分光光度法", "实施", "苟富什"));
        System.out.println("图书列表");

        new Thread(()->{
            int randomInt = RandomUtil.randomInt(0, bookList.size() - 1);
            System.out.println(bookList.get(randomInt));
        });

        new Thread(()->{
            int randomInt = RandomUtil.randomInt(0, bookList.size() - 1);
            System.out.println(bookList.get(randomInt));
        });

        new Thread(()->{
            int randomInt = RandomUtil.randomInt(0, bookList.size() - 1);
            System.out.println(bookList.get(randomInt));
        });
    }


}
